package hgx.spring.cloud.lession.orderserver.service;/**
 * Created by liujun on 2018/3/15.
 */

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import hgx.spring.cloud.lession.common.utils.BeanUtils;
import hgx.spring.cloud.lession.orderserver.dto.OrderDto;
import hgx.spring.cloud.lession.orderserver.po.OrderPo;
import hgx.spring.cloud.lession.orderserver.vo.OrderItemVo;
import hgx.spring.cloud.lession.orderserver.vo.OrderVo;

/**
 * @author Administrator
 * @create 2018-03-15 14:33
 */
@Service
//@Transactional
public class OrderServer {

//    @Resource
//    private OrderDao orderDao;

    public OrderServer() {
        initData();
    }

    public OrderDto createOrder(OrderDto orderDto){
        if(orderDto == null){
            return null;
        }
        OrderPo orderPo=new OrderPo();
        BeanUtils.copyNotNullProperties(orderDto,orderPo);
//        orderDao.save(orderPo);

        orderDto.setId(orderPo.getId());
        return orderDto;

    }

    public OrderVo getOrderVoById(Long id){
        if(id == null || orderVos == null || orderVos.isEmpty()){
            return  null;
        }

        for(OrderVo vo: orderVos){
            if(vo !=null && id.equals(vo.getId())){
                return vo;
            }
        }

        return null;

    }

    public List<OrderVo> findAll(){
        return orderVos;
    }

    private List<OrderVo> orderVos ;

    private void initData(){
        orderVos=new ArrayList<>(2);
        OrderVo orderVo=new OrderVo();
        orderVo.setCreateDate(new Date());
        orderVo.setId(1L);
        orderVo.setStatus((byte)1);
        orderVo.setTotalMoney(265.80f);
        orderVo.setUserId(456L);
        orderVo.setUpdateDate(new Date());

        OrderItemVo orderItemVo1=new OrderItemVo();
        orderItemVo1.setCreateDate(new Date());
        orderItemVo1.setId(111L);
        orderItemVo1.setNum(5);
        orderItemVo1.setPrice(15.36f);
        orderItemVo1.setProductId(77L);

        orderVo.addOrderItemVo(orderItemVo1);

        OrderItemVo orderItemVo2=new OrderItemVo();
        orderItemVo2.setCreateDate(new Date());
        orderItemVo2.setId(222L);
        orderItemVo2.setNum(3);
        orderItemVo2.setPrice(57.26f);
        orderItemVo2.setProductId(66L);

        orderVo.addOrderItemVo(orderItemVo2);

        orderVos.add(orderVo);


        OrderVo orderVo2=new OrderVo();
        orderVo2.setCreateDate(new Date());
        orderVo2.setId(2L);
        orderVo2.setStatus((byte)3);
        orderVo2.setTotalMoney(105.3f);
        orderVo2.setUserId(46L);
        orderVo2.setUpdateDate(new Date());

        OrderItemVo orderItemVo3=new OrderItemVo();
        orderItemVo3.setCreateDate(new Date());
        orderItemVo3.setId(333L);
        orderItemVo3.setNum(7);
        orderItemVo3.setPrice(28.36f);
        orderItemVo3.setProductId(99L);

        orderVo2.addOrderItemVo(orderItemVo3);
        orderVos.add(orderVo2);


    }


}
